#include "formserversettings.h"
#include "ui_formserversettings.h"

#include <QDebug>
#include "nntpclient.h"

FormServerSettings::FormServerSettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::formServerSettings)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);

    databaseManager = DatabaseManager::instance();
    connect(databaseManager, SIGNAL(serverList(ServerInformationList)), this, SLOT(newServerList(ServerInformationList)));
    connect(databaseManager, SIGNAL(serverListChanged()), this, SLOT(refreshServerList()));
    databaseManager->getServerList();

    connect(ui->pushbuttonClose, SIGNAL(clicked()), this, SLOT(close()));
    connect(ui->pushbuttonSave, SIGNAL(clicked()), this, SLOT(saveServer()));
    connect(ui->pushbuttonDelete, SIGNAL(clicked()), this, SLOT(deleteServer()));

    connect(ui->comboboxSelectServer, SIGNAL(activated(int)), this, SLOT(selectServer(int)));
}

FormServerSettings::~FormServerSettings()
{
    delete ui;
}

void FormServerSettings::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void FormServerSettings::newServerList(ServerInformationList servers)
{
    qDebug() << this << "Got server list";
    serverInformationList = servers;

    ui->comboboxSelectServer->clear();

    foreach (ServerInformation *serverInformation, serverInformationList) {
        ui->comboboxSelectServer->addItem(serverInformation->name);
    }

    // Add an item to the combobox for empty settings
    ui->comboboxSelectServer->addItem("Add new server");
    selectServer(ui->comboboxSelectServer->currentIndex());
}

void FormServerSettings::saveServer()
{
    ServerInformation *serverInformation = new ServerInformation;
    serverInformation->adres = ui->lineeditAdres->text();
    serverInformation->port = ui->lineeditPort->text().toInt();
    serverInformation->name = ui->lineeditCustomName->text();
    serverInformation->username = ui->lineeditUsername->text();
    serverInformation->password = ui->lineeditPassword->text();
    databaseManager->addServer(serverInformation);

    NNTPClient *client = new NNTPClient;
    client->setServerInformation(serverInformation);
    client->start();
}

void FormServerSettings::deleteServer()
{
    ServerInformation *serverInformation = new ServerInformation;
    serverInformation->adres = ui->lineeditAdres->text();
    serverInformation->port = ui->lineeditPort->text().toInt();
    serverInformation->name = ui->lineeditCustomName->text();
    serverInformation->username = ui->lineeditUsername->text();
    serverInformation->password = ui->lineeditPassword->text();
    databaseManager->removeServer(serverInformation);
}

void FormServerSettings::refreshServerList()
{
    databaseManager->getServerList();
}

void FormServerSettings::selectServer(int serverId)
{
    QString adres;
    QString port;
    QString name;
    QString username;
    QString password;

    if (serverId == ui->comboboxSelectServer->count() - 1) {
        adres = QString("");
        port = QString("");
        name = QString("");
        username = QString("");
        password = QString("");
        ui->pushbuttonDelete->setEnabled(false);
    } else {
        ServerInformation *serverInformation = serverInformationList.at(serverId);
        adres = serverInformation->adres;
        port = QString::number(serverInformation->port);
        name = serverInformation->name;
        username = serverInformation->username;
        password = serverInformation->password;
        ui->pushbuttonDelete->setEnabled(true);
    }

    ui->lineeditAdres->setText(adres);
    ui->lineeditPort->setText(port);
    ui->lineeditCustomName->setText(name);
    ui->lineeditUsername->setText(username);
    ui->lineeditPassword->setText(password);
}
